//
//  NewSongViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "NewSongViewController.h"
#import "MoreAlbumCollectionViewCell.h"
#import "ListViewController.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefresh.h"


@interface NewSongViewController () <UICollectionViewDataSource, UICollectionViewDelegate>


@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)UICollectionView *albumCollectionView;
@property (nonatomic, retain)NSMutableArray *albumArr;
@property (nonatomic, retain)NSString *nextPageAddress;
@property (nonatomic, assign)NSInteger page;

@end

@implementation NewSongViewController

- (void)dealloc
{
    [_nextPageAddress release];
    [_albumCollectionView release];
    [_albumArr release];
    [_HUD release];
    [_addressStr release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    PlayView *playView = (PlayView*)[window viewWithTag:10001];
    playView.target = self.navigationController;
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 release];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, 44)];
    UIImage *image1 = [UIImage imageNamed:@"bg2.jpg"];
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
    imageView1.userInteractionEnabled = YES;
    imageView1.image = image1;
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [leftButton addSubview:imageView];
    [imageView release];
    
    [leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView1 addSubview:leftButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, VIEW_WIDTH - 200, 44)];
    titleLabel.text = @"最新歌曲";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    [titleLabel release];
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
    flowLaout.itemSize = CGSizeMake((VIEW_WIDTH - 30) / 2, (VIEW_WIDTH - 10) / 2 + 40);
    flowLaout.minimumInteritemSpacing = 10;
    flowLaout.minimumLineSpacing = 10;
    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLaout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    CGFloat ab = 70.0 / 667;
    
    self.albumCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 - ab * VIEW_HEIGHT) collectionViewLayout:flowLaout];
    self.albumCollectionView.dataSource = self;
    self.albumCollectionView.delegate = self;
    self.albumCollectionView.showsVerticalScrollIndicator = NO;
    self.albumCollectionView.backgroundColor = [UIColor whiteColor];
    [self.albumCollectionView registerClass:[MoreAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"MoreAlbumCollectionReuse"];
    
    [self.albumCollectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(pullAction)];
    [self.view addSubview:self.albumCollectionView];
    [_albumCollectionView release];
    
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.albumCollectionView];
    [self.albumCollectionView addSubview:self.HUD];
    self.HUD.labelText = @"Music is on the way...";
    self.HUD.dimBackground = YES;
    [self.HUD show:YES];
    
    [NetHandler getDataWithUrl:self.addressStr completion:^(id dict) {
        self.albumArr = [NSMutableArray array];
        NSArray *arr = [[[[dict objectForKey:@"plaze_album_list"] objectForKey:@"RM"] objectForKey:@"album_list"] objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            Album *album = [[Album alloc] initWithDictionary:dic];
            [self.albumArr addObject:album];
            [album release];
        }
        [self.albumCollectionView reloadData];
        [self.HUD hide:YES];
    }];
    
    
    
}

- (void)pullAction
{
    self.nextPageAddress = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.plaza.getRecommendAlbum&format=json&offset=%ld&limit=30", self.page * 30 + 60];
    self.page++;
    [NetHandler getDataWithUrl:self.nextPageAddress completion:^(id dict) {
            NSArray *arr = [[[[dict objectForKey:@"plaze_album_list"] objectForKey:@"RM"] objectForKey:@"album_list"] objectForKey:@"list"];
        if ([arr isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dic in arr) {
                Album *album = [[Album alloc] initWithDictionary:dic];
                [self.albumArr addObject:album];
                [album release];
            }
            [self.albumCollectionView reloadData];
            [self.albumCollectionView.footer endRefreshing];
        }
        else {
            self.albumCollectionView.footer.state = MJRefreshFooterStateNoMoreData;
        }
        
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.albumArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreAlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreAlbumCollectionReuse" forIndexPath:indexPath];
    cell.album = [self.albumArr objectAtIndex:indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ListViewController *listVC = [[ListViewController alloc] init];
    Album *album = [self.albumArr objectAtIndex:indexPath.row];
    listVC.addressStr = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.album.getAlbumInfo&format=json&album_id=%@",album.album_id];
    [self.navigationController pushViewController:listVC animated:YES];
    [listVC release];
    
}

- (void)leftButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIButton *button = (UIButton *)[window viewWithTag:1000];
    button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
